package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.RequipmentMaintRec;
import com.ruoyi.business.mapper.RequipmentMaintRecMapper;
import com.ruoyi.business.service.IRequipmentMaintRecService;
import com.ruoyi.business.vo.EquipmentMaintenanceSumVO;
import com.ruoyi.business.vo.EquipmentMaintenanceVO;
import com.ruoyi.business.vo.RequipmentMaintRecListVO;
import com.ruoyi.business.vo.StatisticVo;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 设备维护记录Service业务层处理
 * 
 * @author qiancheng
 * @date 2025-09-29
 */
@Service
public class RequipmentMaintRecServiceImpl extends ServiceImpl<RequipmentMaintRecMapper, RequipmentMaintRec> implements IRequipmentMaintRecService
{
    @Autowired
    private RequipmentMaintRecMapper requipmentMaintRecMapper;

    /**
     * 查询设备维护记录
     * 
     * @param id 设备维护记录主键
     * @return 设备维护记录
     */
    @Override
    public RequipmentMaintRec selectRequipmentMaintRecById(Long id)
    {
        return requipmentMaintRecMapper.selectRequipmentMaintRecById(id);
    }

    /**
     * 查询设备维护记录列表
     * 
     * @param requipmentMaintRec 设备维护记录
     * @return 设备维护记录
     */
    @Override
    public List<RequipmentMaintRec> selectRequipmentMaintRecList(RequipmentMaintRec requipmentMaintRec)
    {
        return requipmentMaintRecMapper.selectRequipmentMaintRecList(requipmentMaintRec);
    }

    /**
     * 新增设备维护记录
     * 
     * @param requipmentMaintRec 设备维护记录
     * @return 结果
     */
    @Override
    public int insertRequipmentMaintRec(RequipmentMaintRec requipmentMaintRec)
    {
        requipmentMaintRec.setCreateTime(DateUtils.getNowDate());
        // 如果维护时间不为空，则拆分年月日
        if (requipmentMaintRec.getMaintenanceTime() != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(requipmentMaintRec.getMaintenanceTime());
            requipmentMaintRec.setRecordYear((long) calendar.get(Calendar.YEAR));
            requipmentMaintRec.setRecordMonth((long) (calendar.get(Calendar.MONTH) + 1)); // 月份从0开始，要+1
            requipmentMaintRec.setRecordDay((long) calendar.get(Calendar.DAY_OF_MONTH));
        }

        return requipmentMaintRecMapper.insert(requipmentMaintRec);
    }

    /**
     * 修改设备维护记录
     * 
     * @param requipmentMaintRec 设备维护记录
     * @return 结果
     */
    @Override
    public int updateRequipmentMaintRec(RequipmentMaintRec requipmentMaintRec)
    {
        requipmentMaintRec.setUpdateTime(DateUtils.getNowDate());
        return requipmentMaintRecMapper.updateRequipmentMaintRec(requipmentMaintRec);
    }

    /**
     * 批量删除设备维护记录
     * 
     * @param ids 需要删除的设备维护记录主键
     * @return 结果
     */
    @Override
    public int deleteRequipmentMaintRecByIds(Long[] ids)
    {
        return requipmentMaintRecMapper.deleteRequipmentMaintRecByIds(ids);
    }

    /**
     * 删除设备维护记录信息
     * 
     * @param id 设备维护记录主键
     * @return 结果
     */
    @Override
    public int deleteRequipmentMaintRecById(Long id)
    {
        return requipmentMaintRecMapper.deleteRequipmentMaintRecById(id);
    }

    /**
     * 查询维养记录列表
     * @param params
     * @return
     */
    @Override
    public List<RequipmentMaintRecListVO> selectMaintRecList(RequipmentMaintRec params) {
        return requipmentMaintRecMapper.selectMaintRecList(params);
    }

    /**
     * 查询设备维护次数信息
     *
     * @param year   年份
     * @param rackId
     * @return 包含各设备维护次数和总次数的列表
     */
    @Override
    public EquipmentMaintenanceSumVO getEquipmentMaintenanceCount(Integer year, String rackId) {
        List<EquipmentMaintenanceVO> countList = baseMapper.getEquipmentMaintenanceCount(year,rackId);
        // 计算所有设备的维护总次数（使用Java 8 Stream）
        int totalMaintenanceCount = countList.stream()
                // 过滤掉维护次数为null的记录（避免空指针）
                .filter(vo -> vo.getMaintenanceCount() != null)
                // 提取维护次数
                .mapToInt(EquipmentMaintenanceVO::getMaintenanceCount)
                // 求和
                .sum();
        return new EquipmentMaintenanceSumVO(totalMaintenanceCount,countList);
    }

    /**
     * 获取完整的月度统计数据（自动补零）
     * @return
     */
    @Override
    public List<StatisticVo> getFullMonthStatistic() {

        // 1. 定义所有维度：4类数据 × 2个月份
        List<Integer> allDataTypes = Arrays.asList(1, 2, 3, 4);
        List<Integer> allMonths = Arrays.asList(1, 2);

        // 2. 初始化完整维度（默认count=0）
        List<StatisticVo> fullResult = new ArrayList<>();
        for (Integer dataType : allDataTypes) {
            for (Integer month : allMonths) {
                StatisticVo vo = new StatisticVo();
                vo.setDataType(dataType);
                vo.setMonthGroup(month);
                vo.setCount(0);
                fullResult.add(vo);
            }
        }

        // 3. 查询数据库有数据的维度
        List<StatisticVo> fullMonthStatistic = baseMapper.getFullMonthStatistic();

        // 4. 覆盖有数据的维度
        for (StatisticVo dbVo : fullMonthStatistic) {
            for (StatisticVo fullVo : fullResult) {
                if (dbVo.getDataType().equals(fullVo.getDataType())
                        && dbVo.getMonthGroup().equals(fullVo.getMonthGroup())) {
                    fullVo.setCount(dbVo.getCount());
                    break;
                }
            }
        }
        return fullResult;
    }

}
